#define DEBUG
#include <cstdio>

using namespace std;

typedef unsigned int ui;
const int maxn=5;

class Matrix {
public:
  ui v[1<<maxn][1<<maxn], n, m;

  void initOne(int n) {
    this->n = m = n;
    for (int i=0; i<n; i++) {
      for (int j=0; j<n; j++) {
	v[i][j] = 0;
      }
      v[i][i] = 1;
    }
  }

  void mul(Matrix &a, Matrix &b) {
    n=a.n, m=b.m;
    for (ui i=0; i<n; i++) {
      for (ui j=0; j<m; j++) {
	v[i][j] = 0;
	for (ui k=0; k<a.m; k++) {
	  v[i][j] += a.v[i][k]*b.v[k][j];
	}
      }
    }
  }

  void qpower(int n) {
    static Matrix t[2], s[2];
    t[1] = *this;
    s[1].initOne(m);
    int i=1, j=1;
    for (; n>1; n/=2) {
      if (n%2) {
	j ^= 1;
	s[j].mul(s[!j], t[i]);
      }
      i ^=1;
      t[i].mul(t[!i], t[!i]);
    }
    mul(t[i], s[j]);
  }
};

void initMatrix(Matrix &a, int n, bool set[][30], int to[][2*maxn+1]) {
  a.n = a.m = 1<<n;
  for (int i=0; i<(1<<n); i++) {
    for (int c=0; c<26; c++) {
      int y=0;
      for (int j=1; j<=n; j++) {
	if ((i>>(j-1))&1) {
	  for (int k=1; k<=to[j][0]; k++) {
	    if (set[to[j][k]][c]) {
	      y |= 1<<(to[j][k]-1);
	    }
	  }
	}
      }
      if (y) {
	a.v[i][y]++;
      }
    }
  }
}

int main() {
#ifdef DEBUG
  freopen("src.in", "r", stdin);
  freopen("src.out", "w", stdout);
#endif

  static char patt[1000];
  static bool set[maxn+1][30];
  static int to[maxn+1][2*maxn+1], pair[4*maxn+1];
  int n=0;
  scanf("%s", patt);
  for (int i=0; patt[i]; i++) {
    if (patt[i]=='(') {
      pair[++pair[0]] = n+1;
    } else if (patt[i]==')') {
      to[n][++to[n][0]] = pair[pair[0]--];
    } else if (patt[i]=='[') {
      to[n][++to[n][0]] = n+1;
      n++;
    } else if (patt[i]>='a' && patt[i]<='z') {
      set[n][patt[i]-'a'] = true;
    }
  }

  static Matrix end, t, init;
  initMatrix(t, n, set, to);
  int m;
  scanf("%d", &m);
  init.n = 1, init.m = 1<<n;
  for (int i=0; i<26; i++) {
    init.v[0][1] += set[1][i];
  }
  t.qpower(m-1);
  end.mul(init, t);

  ui ans=0;
  for (int i=0; i<(1<<n); i++) {
    if ((i>>(n-1))&1) {
      ans = ans+end.v[0][i];
    }
  }
  printf("%u", ans);

  fclose(stdin);
  fclose(stdout);
  return 0;
}
